package cn.edu360.streaming.utils;

import cn.edu360.streaming.searcher.ResourceMatcherSearcher;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * wzxjava@126.com
 * Created by wangzhixuan on 2017/05/18 21:15
 */
public class DefaultRegUtil {

    public static Map<String, String> loadPatterns() {
        Set<String> setFile = ResourceMatcherSearcher.getFiles(new String[]{"patterns"}, "*");
        Map<String, String> patterns = new HashMap<>();
        ClassLoader classLoader = ClassUtil.getClassLoader();
        for (String resource : setFile) {
            InputStream is = classLoader.getResourceAsStream(resource);
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String sCurrentLine;

                while ((sCurrentLine = br.readLine()) != null) {
                    sCurrentLine = sCurrentLine.trim();
                    if (sCurrentLine.length() == 0
                            || sCurrentLine.indexOf("#") == 0) {
                        continue;
                    }
                    patterns.put(sCurrentLine.split("\\s", 2)[0],
                            sCurrentLine.split("\\s", 2)[1]);
                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            IOUtil.closeQuietly(is);
        }
        return patterns;
    }
}
